package com.smart.car.record.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.smart.car.common.res.bean.ResponseResult;
import com.smart.car.common.res.exception.BusinessException;
import com.smart.car.record.entity.RecordChargeRule;
import com.smart.car.record.service.RecordChargeRuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO 计费规则控制
 * @author: zoutao.blog.csdn.net
 * @date: 2021-01-06
 */
@RestController
@RequestMapping("chargingRule")
public class RecordChargeRuleController {

    @Autowired
    private RecordChargeRuleService ruleService;


    /**
     * 查询规则列表(不使用缓存示例)
     * @author zoutao.blog.csdn.net
     */
    @GetMapping("/list")
    public ResponseResult list(){
        // 从数据库中查询数据
        List<RecordChargeRule> list = ruleService.list();
        return ResponseResult.ok(list);
    }

    /**
     * 查询规则列表(使用本地缓存示例)
     * @author zoutao.blog.csdn.net
     */
    private Map<String, Object> cache = new HashMap<>();
    @GetMapping("/cacheList")
    public ResponseResult listLocal(){
        List<RecordChargeRule> ruleCache = (List<RecordChargeRule>) cache.get("ruleList");
        // 如果缓存中没有，则先从数据库中获取
        if (ruleCache == null) {
            System.out.println("缓存为空");
            // 从数据库中查询数据
            List<RecordChargeRule> list = ruleService.list();
            // 将数据放入缓存中
            ruleCache = list;
            cache.put("ruleList", list);
        }
        System.out.println("缓存的值是：》》》》》》》》》》》》"+cache.get("ruleList"));
        return ResponseResult.ok(ruleCache);
    }


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询规则列表(使用redis缓存示例)
     * @author zoutao.blog.csdn.net
     */
    @GetMapping("/listByRedis")
    public List<RecordChargeRule> listRedis() {
        // 1.初始化 redis 组件
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        // 2.从缓存中查询数据
        String ruleList = ops.get("ruleList");
        // 3.如果缓存中没有数据
        if (StringUtils.isEmpty(ruleList)) {
            // 4.从数据库中查询数据
            List<RecordChargeRule> DbList = ruleService.list();
            // 5.将从数据库中查询出的数据序列化 JSON 字符串
            ruleList = JSON.toJSONString(DbList);
            // 6.将序列化后的数据存入缓存中
            ops.set("ruleList", ruleList);
            return DbList;
        }
        // 7.如果缓存中有数据，则从缓存中拿出来，并反序列化为实例对象
        List<RecordChargeRule> entityList = JSON.parseObject(ruleList, new TypeReference<List<RecordChargeRule>>(){});
        return entityList;
    }


    /**
     * TODO 更改收费规则时，使用此方法刷新数据的缓存
     * @author zoutao.blog.csdn.net
     */
    @PostMapping("/fresh")
    public ResponseResult<Integer> refresh() throws BusinessException {
        ResponseResult<Integer> result = new ResponseResult<>();
        int rtn = ruleService.refresh();
        result.setMsg("更新成功");
        result.setCode(rtn);
        return result;
    }

    /**
     * 新增
     * @author zoutao.blog.csdn.net
     **/
    @RequestMapping("/insert")
    public Object insert(RecordChargeRule recordChargeRule){
        return ruleService.insert(recordChargeRule);
    }

    /**
     * 刪除
     * @author zoutao.blog.csdn.net
     **/
    @RequestMapping("/delete")
    public ResponseResult<String> delete(String id){
        return ruleService.delete(id);
    }

    /**
     * 更新
     * @author zoutao.blog.csdn.net
     **/
    @RequestMapping("/update")
    public ResponseResult<String> update(RecordChargeRule recordChargeRule){
        return ruleService.update(recordChargeRule);
    }

    /**
     * 查询 根据主键 id 查询
     * @author zoutao.blog.csdn.net
     **/
    @RequestMapping("/load")
    public Object load(String id){
        return ruleService.load(id);
    }

}
